<?php
/**
 * @author Biju T biju@poornam.com
 *  
 */
function __autoload($classname) {
	require_once(FRAMEWORKPATH."/classes/impl/".$classname.".php");
}
require_once("classes/impl/YAMLConfigManager.php");
require_once("classes/impl/HttpRequestWrapper.php");
require_once("classes/impl/HttpResponseWrapper.php");

class FrameWork{
	private $frameWork = null;
	private static $conf = null;
	private static $listner = null;
	private static $tpl = null;
	
	function __construct(){
		//Load Config
		if(self::$conf == null){
			self::$conf = new YAMLConfigManager();
			self::$conf->loadConfig(dirname(__FILE__)."/../../config.yaml");
		}
		
		
	}
	
	function initializeListner($request,$respose,$server,$srequest){
		
		$request->setRequest($server,$srequest);
		$respose->setResponseStream(self::$conf);
		
		foreach(self::$conf->config['urlpatterns'] as $urlpatterns){
			$key = array_search('/'.$request->rurl,$urlpatterns);
			if($key == "url"){ //Found the url pattern in config
				$scriptname = $urlpatterns['script-name'];
				break; 
			}
		}
		if($scriptname != null){ //Found url pattern. Find class from scripts
			foreach(self::$conf->config['scripts'] as $scripts){
				$key = array_search($scriptname,$scripts);
				if($key == "script-name"){ //Found the url pattern in config
					$path = $scripts['class-path'];
					$class = $scripts['class'];
					break; 
				}
			}
			if($class != null){
				require_once(ROOTPATH.'/'.$path.$class.'.php');	
				if($request->method == 'GET'){
					$obj = new $class;
					$obj->doGet($request,$respose);
				}elseif($request->method == 'POST') {
					$obj = new $class;
					$obj->doPost($request,$respose);
				}
			}
		}else{ //Could't find in url pattern.  
			echo "Soory...URL pattern ".$request->rurl." not found in config.yaml";
		}
		
		if($request->method == 'GET'){
			
		}
		if(self::$listner == null){}
	}
	
	function import($location){
		echo $location;
		$loc_arr = split('[/.-]', $location);
		print_r($loc_arr);
		$path = "/";
		foreach($loc_arr as $key=>$loc){
			echo $key;
			$path .= $loc;
			if($key < count($loc_arr)-1){
				$path .= "/";  
			}else{
				$path .= ".php";
			}
		}
		require_once($path);
	}
	
}
?>